package org.zjut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zjut.db.LearnConceptDataBase;
import org.zjut.util.CommonUtil;

import javax.servlet.http.HttpServletRequest;

import static org.zjut.util.CommonUtil.getBodyRequest;

/**
 * @author Rising
 * @date 2017/8/28
 * @des
 */
@Controller
public class LearnerConceptController {
    @Autowired
    LearnConceptDataBase learnConceptDataBase;


    /**
     * 添加学习概念
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/addLearnConcept", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addLearnConcept(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(getBodyRequest(request));
        if (request == null) {
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        try {
            return JSON.toJSONString(learnConceptDataBase.addLeanConcept(params));
        } catch (Exception e) {
            return JSON.toJSONString(CommonUtil.setErrorInfo("插入失败"));
        }
    }

    /**
     * 查询学习概念
     * @param request
     * @return
     */
    @RequestMapping(value="/getLearnConcept", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getLearnConcept(HttpServletRequest request){
        JSONObject params = null;
        if(request!=null){
            params = JSON.parseObject(getBodyRequest(request));
        }
        return JSON.toJSONString(learnConceptDataBase.getLeanConcept(params));
    }


    /**
     * 删除学习概念
     * @param request
     * @return
     */
    @RequestMapping(value="/delLearnConcept", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delLearnConcept(HttpServletRequest request){
        JSONObject params = JSON.parseObject(getBodyRequest(request));
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        try{
            return JSON.toJSONString(learnConceptDataBase.delLearnConcept(params));
        }catch (Exception e){
            return JSON.toJSONString(CommonUtil.setErrorInfo("删除失败"));
        }
    }

    /**
     * 修改学习概念
     * @param request
     * @return
     */
    @RequestMapping(value="/editLearnConcept", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String editLearnConcept(HttpServletRequest request){
        JSONObject params = JSON.parseObject(getBodyRequest(request));
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        try{
            return JSON.toJSONString(learnConceptDataBase.editLearnConcept(params));
        }catch (Exception e){
            return JSON.toJSONString(CommonUtil.setErrorInfo("修改失败"));
        }
    }



    /**
     * 恢复删除的知识点信息
     * @param request
     * @return
     */
    @RequestMapping(value="/rowBackTheDelConcept", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String rowBackTheDelConcept(HttpServletRequest request){
        JSONObject params = JSON.parseObject(getBodyRequest(request));
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        try{
            return JSON.toJSONString(learnConceptDataBase.rowBackTheDelConcept(params));
        }catch (Exception e){
            return JSON.toJSONString(CommonUtil.setErrorInfo("修改失败"));
        }
    }
}
